package com.ruipengkj.commons.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-8-24
 * Time: 下午1:17
 * To change this template use File | Settings | File Templates.
 */
public class CRUDList<T> implements List<T> {
    public enum CRUDStatus {CRUDNone, CRUDModitied, CRUDInserted, CRUDDeleted}
    class CRUDObject<T>
    {
        T obj;
        CRUDStatus status;

        CRUDObject(T obj, CRUDStatus _status) {
            this.obj = obj;
            status = _status;
        }

        public T getObj() {
            return obj;
        }

        public void setObj(T obj) {
            this.obj = obj;
        }

        public CRUDStatus getStatus() {
            return status;
        }

        public void setStatus(CRUDStatus status) {
            this.status = status;
        }
    }

    private List<T> list;
    private List<CRUDObject<T>> modifiedList;

    public CRUDList(List<T> _list) {
        list = _list;
        modifiedList = new ArrayList<CRUDObject<T>>(_list != null?_list.size():0);
        for(int i = 0; _list != null && i < _list.size(); i ++)
        {
            modifiedList.add(new CRUDObject<T>(_list.get(i), CRUDStatus.CRUDNone));
        }
    }

    public int sizec()
    {
        return modifiedList.size();
    }

    public T getc(int index)
    {
        return modifiedList.get(index).getObj();
    }

    public CRUDStatus status(int index)
    {
        return modifiedList.get(index).getStatus();
    }

    public boolean modifiedc(T o)
    {
        CRUDObject<T> co = null;
        boolean result = false;
        for(int i = 0; i < modifiedList.size(); i ++)
        {
            co = modifiedList.get(i);
            if(co.getObj() == o)
            {
                if(co.getStatus() == CRUDStatus.CRUDNone)
                {
                    co.setStatus(CRUDStatus.CRUDModitied);
                }
                result = true;
                break;
            }
        }
        return result;
    }

    //根据list中的index索引找到modifiedList相应的索引
    private int getModifiedIndex(int index)
    {
        CRUDObject<T> co = null;
        for(int i = 0, j = 0; i < modifiedList.size(); i ++)
        {
            co = modifiedList.get(i);
            if(co.getStatus() == CRUDStatus.CRUDDeleted)
            {
                continue;
            }
            if(j == index)
            {
                return j;
            }
            j ++;
        }
        throw new RuntimeException("数据索引错乱");
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public boolean contains(Object o) {
        //return list.contains(o);
        throw new RuntimeException("没有实现");
    }

    @Override
    public Iterator<T> iterator() {
        //return list.iterator();
        throw new RuntimeException("没有实现");
    }

    @Override
    public Object[] toArray() {
        //return list.toArray();
        throw new RuntimeException("没有实现");
    }

    @Override
    public <T> T[] toArray(T[] a) {
        //return list.toArray(a);
        throw new RuntimeException("没有实现");
    }

    public boolean add(T t) {
        boolean result = list.add(t);
        modifiedList.add(new CRUDObject<T>(t, CRUDStatus.CRUDInserted));
        return result;
    }

    @Override
    public boolean remove(Object o) {
        boolean result = list.remove(o);
        CRUDObject<T> co = null;
        for(int i = 0; result && (i < list.size()); i ++)
        {
            co = modifiedList.get(i);
            if(co.getObj() == o)
            {
                if(co.getStatus() == CRUDStatus.CRUDInserted)
                {
                    modifiedList.remove(i);
                }
                else
                {
                    co.setStatus(CRUDStatus.CRUDDeleted);
                }
                break;
            }
        }
        return result;
    }

    @Override
    public boolean containsAll(Collection<?> c) {
        //return list.containsAll(c);
        throw new RuntimeException("没有实现");
    }

    public boolean addAll(Collection<? extends T> c) {
        //return list.addAll(c);
        throw new RuntimeException("没有实现");
    }

    public boolean addAll(int index, Collection<? extends T> c) {
        //return list.addAll(index, c);
        throw new RuntimeException("没有实现");
    }

    @Override
    public boolean removeAll(Collection<?> c) {
        //return list.removeAll(c);
        throw new RuntimeException("没有实现");
    }

    @Override
    public boolean retainAll(Collection<?> c) {
        //return list.retainAll(c);
        throw new RuntimeException("没有实现");
    }

    @Override
    public void clear() {
        throw new RuntimeException("没有实现");
    }

//    @Override
//    public boolean equals(Object o) {
//        return list.equals(o);
//    }
//
//    @Override
//    public int hashCode() {
//        return list.hashCode();
//    }

    @Override
    public T get(int index) {
        return list.get(index);
    }

    public T set(int index, T element) {
        throw new RuntimeException("没有实现");
    }

    public void add(int index, T element) {
        list.add(index, element);
        modifiedList.add(getModifiedIndex(index), new CRUDObject<T>(element, CRUDStatus.CRUDInserted));
    }

    @Override
    public T remove(int index) {
        T result = list.remove(index);
        int modifiedIndex = getModifiedIndex(index);
        CRUDObject<T> co = modifiedList.get(modifiedIndex);
        if(co.getStatus() == CRUDStatus.CRUDInserted)
        {
            modifiedList.remove(modifiedIndex);
        }
        else
        {
            co.setStatus(CRUDStatus.CRUDDeleted);
        }
        return result;
    }

    @Override
    public int indexOf(Object o) {
        //return list.indexOf(o);
        throw new RuntimeException("没有实现");
    }

    @Override
    public int lastIndexOf(Object o) {
        //return list.lastIndexOf(o);
        throw new RuntimeException("没有实现");
    }

    @Override
    public ListIterator<T> listIterator() {
        //return list.listIterator();
        throw new RuntimeException("没有实现");
    }

    @Override
    public ListIterator<T> listIterator(int index) {
        //return list.listIterator(index);
        throw new RuntimeException("没有实现");
    }

    @Override
    public List<T> subList(int fromIndex, int toIndex) {
        //return list.subList(fromIndex, toIndex);
        throw new RuntimeException("没有实现");
    }
}
